﻿-- =============================================
-- Author:		<赵洪涛>
-- Create date: <2012-08-20>
-- Description:	<接口人获取待下发的客户>
-- =============================================
CREATE PROCEDURE [dbo].[proc_InOutSide_GetCustomerList]
(
	@CompanyId int,
	@StartIndex int,
	@EndIndex int
)
AS
BEGIN
	;WITH list As (Select ROW_NUMBER() OVER (ORDER BY In_Date Desc) AS Row,
		A.Id,
		C.delFlag,
		C.CustomerId,
		C.CustomerName,
		C.Project,
		C.ProtectId,
		IsNull((Select Title From Customer_Source Where Id=C.SourceId),'...') As Source,
		A.InSide_ProtectDate As ProtectDate,
		IsNull((Select Title From Customer_Industry Where Id=C.IndustryOneId),'...') As IndustryOne,
		IsNull((Select Title From Customer_Industry Where Id=C.IndustryTwoId),'...') As IndustryTwo,
		IsNull((Select Title From Customer_Type Where Id=C.TypeId),'...') As Type,
		IsNull((Select Top 1 EName From Employee Where Employee.UserName=A.InSide),'') As SalesPerson,
		A.In_Date As TrackDate
	From InOutSide A,Customer C
	Where A.Companyid=@CompanyId And A.IfSide='' And A.CusId=C.CustomerId
		And C.Audit=1 And C.Companyid=@CompanyId)

	Select *,(Select Count(0) From List) As RecordCount From List Where Row Between @StartIndex And @EndIndex Order By Row
END
